//
//  SLInterview_AnimationController.m
//  Objective-C
//
//  Created by S_Line on 2018/8/17.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLInterview_AnimationController.h"

typedef NS_ENUM(NSInteger, AnimationDirection) {
    AnimationDirectionLeft,
    AnimationDirectionRight,
    AnimationDirectionTop,
    AnimationDirectionBottom,
};

API_AVAILABLE(ios(10.0))
@interface SLInterview_AnimationController ()

@property (nonatomic, assign) AnimationDirection animationDirection;

@property (nonatomic, strong) UIViewPropertyAnimator *animator;
@property (nonatomic, assign) CGFloat progress;

@property (nonatomic, weak) UIView *ballView;

@end

@implementation SLInterview_AnimationController

- (void)initNavBar {
    self.navTitle = @"动画";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initData];
    
    [self initView];
}

- (void)initData {
    self.animationDirection = AnimationDirectionRight;
}

- (void)initView {
    
    
    UIView *ballView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 50, 50)];
    self.ballView = ballView;
    __weak SLInterview_AnimationController *weakSelf = self;
    ballView.tapBlock = ^{
        
        CGFloat ballViewX = weakSelf.ballView.frame.origin.x;
        CGFloat offsetX = ballViewX + 200 > kScreenWidth ? -200 : 200;
        CGRect ballViewF = weakSelf.ballView.frame;
        ballViewF.origin.x = ballViewF.origin.x + offsetX;
        
        // UIViewPropertyAnimator实现
//        if (@available(iOS 10.0, *)) {
//            UIViewPropertyAnimator *animator = [[UIViewPropertyAnimator alloc] initWithDuration:1.0f curve:UIViewAnimationCurveLinear animations:^{
//                weakSelf.ballView.frame = ballViewF;
//            }];
//            [animator startAnimation];
//        }
        
        // UIView Animation实现
//        [UIView animateWithDuration:1.0 animations:^{
//            weakSelf.ballView.frame = ballViewF;
//        }];
        
        // CALayer 实现
//        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
//        basicAnimation.fromValue = [[NSNumber alloc] initWithDouble:weakSelf.ballView.center.x];
//        basicAnimation.toValue = [[NSNumber alloc] initWithDouble:ballViewF.origin.x + ballViewF.size.width / 2.0];
//        basicAnimation.duration = 1.0f;
//        [weakSelf.ballView.layer addAnimation:basicAnimation forKey:nil];
    };
    ballView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:ballView];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handelPan:)];
    [self.view addGestureRecognizer:pan];
    
}

- (void)handelPan:(UIPanGestureRecognizer *)gesture {
    
    CGPoint translation = [gesture translationInView:self.ballView];
    SLog(@"translation: %@", NSStringFromCGPoint(translation));
    CGPoint velocity = [gesture velocityInView:self.ballView];
    SLog(@"velocity: %@", NSStringFromCGPoint(velocity));
    
    self.ballView.center = CGPointMake(self.ballView.center.x +translation.x , self.ballView.center.y + translation.y);
    //每次移动完，将移动量置为0，否则下次移动会加上这次移动量
    [gesture setTranslation:CGPointMake(0, 0) inView:self.ballView];
    
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:{
            [self.animator stopAnimation:true];
//            self.progress = self.animator.fractionComplete;
            SLog(@"Began");
        }
            break;
        case UIGestureRecognizerStateChanged:{
//            self.animator.fractionComplete = translation.x / 200.0 + self.progress;
            SLog(@"Changed");
        }
            break;
        case UIGestureRecognizerStateEnded:{
            __weak typeof(self) weakSelf = self;
            if (@available(iOS 10.0, *)) {
                self.animator = [[UIViewPropertyAnimator alloc] initWithDuration:1.0 curve:UIViewAnimationCurveEaseOut animations:^{
                    CGRect ballViewF = weakSelf.ballView.frame;
                    ballViewF.origin.x = ballViewF.origin.x + 100;
                    weakSelf.ballView.frame = ballViewF;
                }];
            }
            [self.animator startAnimation];
            SLog(@"Ended");
        }
        default:
            break;
    }
}

@end
